package com.gym.code.config.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gym.code.config.admin.bean.db.DBBaseMappingInfo;
import com.gym.code.config.admin.bean.db.DBMappingInfo;
import com.gym.code.config.admin.bean.table.TableMappingBean;
import com.gym.code.config.admin.dao.IDBMappingDao;
import com.gym.code.config.admin.service.IDBMappingService;
import com.gym.code.config.admin.service.ITableMappingService;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-4-11 下午4:37:41
 */
@Service
public class DBMappingService implements IDBMappingService{

	@Autowired
	private IDBMappingDao dbMappingDao;
	
	@Autowired
	private ITableMappingService tableMappingService;
	
	@Override
	public int insert(DBBaseMappingInfo bean) {
		return dbMappingDao.insert(bean);
	}

	@Override
	public int update(DBBaseMappingInfo bean) {
		return dbMappingDao.update(bean);
	}

	@Override
	public DBMappingInfo query(long id) {
		DBBaseMappingInfo baseBean = queryBase(id); 
		return wrapDBMappingInfo(baseBean);
	}

	@Override
	public List<DBBaseMappingInfo> queryBaseAll() {
		return dbMappingDao.queryAll();
	}

	@Override
	public int delete(long id) {
		return dbMappingDao.delete(id);
	}

	@Override
	public DBBaseMappingInfo queryBase(long id) {
		return dbMappingDao.query(id);
	}

	
	private DBMappingInfo wrapDBMappingInfo(DBBaseMappingInfo baseBean ){
		DBMappingInfo result = new DBMappingInfo(baseBean);
		List<TableMappingBean> tableList = tableMappingService.queryAll(baseBean.getId());
		result.getTableList().addAll(tableList);
		return result;
	}
	
	@Override
	public List<DBMappingInfo> queryAll() {
		List<DBBaseMappingInfo>  baseList = queryBaseAll();
		List<DBMappingInfo> result = new ArrayList<DBMappingInfo>();
		for(DBBaseMappingInfo item : baseList){
			result.add(wrapDBMappingInfo(item));
		}
		return result;
	}
	
}
